<?php
/**
 * Copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author francissem
 * @copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */
require_once("Core/UrlQueryParams.php");
require_once("Core/Module.php");
require_once("Zend/Locale.php");
class Core_Controller_Request_Abstract extends ArrayObject{

	protected $all_site_tags;
	protected $site_tag;
	protected $lang_tag;
	protected $module_url;
	protected $module_name;
	protected $request_url;
	protected $cropped_url;

	function __construct($headers,$all_site_tags){
		parent::__construct($headers);
		$this->request_url = $this["REDIRECT_URL"];
			
		$this->all_site_tags = $all_site_tags;
		$cropped_url = pathjoin("/",preg_replace("/^".str_replace('/','\/',pathjoin("/",Core_System::GetSetting("general","url_base")))."/","",$headers["REDIRECT_URL"]));
		$match_wtag=array();
		$stags = implode('|',$this->all_site_tags);
		if(preg_match("/^\/({$stags})\/.*/",$cropped_url,$match_wtag)){
			$cropped_url = preg_replace("/^\/({$stags})/","",$cropped_url);
			$this->site_tag = $match_wtag[1];
		}
			
		$ns=explode('/',$cropped_url);
			
		if(count(preg_grep("/".current(array_slice($ns,1,1))."/i",array_keys(Zend_Locale::getLocaleList())))){
			$this->lang_tag=current(array_slice($ns,1,1));
			$cropped_url = pathjoin('/',implode('/',array_slice($ns,2)));
		}

		$modules_info = Core_Module::getAllModuleInfo();
		$match_modules =array();
			
		foreach((Core_Module::getModuleUrlMap()) as $url=>$mname){
			if(preg_match("/^".str_replace('/','\/',$url)."\/?/",$cropped_url)) $match_modules[]=$mname;
		}
			
		if(count($match_modules)>1){
			$s=9999;
			$selected_module_name=null;
			foreach($match_modules as $n){
				if($s>($ss=count(split('/',$n)))) {
					$s = $ss;
					$selected_module_name=$n;
				}
			}
			$this->module_name = $selected_module_name;
		}elseif(count($match_modules)==1){
			$this->module_name=current($match_modules);
		}else{
			require_once("Core/Controller/FileNotFoundException.php");
			throw new Core_Controller_FileNotFoundException("File Not Found 404");
		}
		$this->cropped_url=$cropped_url;
			
	}

	function getSiteTag(){
		return $this->site_tag;
	}

	function getLanguageTag(){
		return $this->lang_tag;
	}

	function getModuleName(){
		return $this->module_name;
	}

	function getModuleUrl(){
		static $mu;
		if(!isset($mu)) {
			$mu = pathjoin("/",$this->site_tag,$this->lang_tag,$this->module_url);
		}
		return $mu;
	}

	function getQueryParams(){
		static $pquery;
		if(!isset($pquery)){
			$pquery = new Core_UrlQueryParams($this["QUERY_STRING"]);
		}
		return $pquery;
	}
}
?>